<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );  

require_once ( 'ps_database.php');

class vm_ps_cleardata {
    function processClear($vendor_id) {         
        // Delete product_price by vendor id
        ps_cleardata::deleteProductPrice($vendor_id);
        
        // Delete product_mx_xref by vendor id
        ps_cleardata::deleteProductMfXref($vendor_id);
        
        // Delete category_xref by vendor id
        ps_cleardata::deleteCategoryXref($vendor_id);
        
        // Delete category by vendor id
        ps_cleardata::deleteCategory($vendor_id);
        
        // Delete product_category_xref by vendor id
        ps_cleardata::deleteProductCategoryXref($vendor_id);
        
        // Delete product by vendor id
        ps_cleardata::deleteProduct($vendor_id);
    }
        
    function deleteProductPrice($vendor_id) {
        $q = "DELETE FROM #__{vm}_product_price WHERE product_id IN (SELECT product_id FROM #__{vm}_product WHERE vendor_id = ".$vendor_id.")";
        
        $db = new ps_DB;      
        $db->query( $q );
    }
    
    function deleteProductMfXref($vendor_id) {
        $q = "DELETE FROM #__{vm}_product_mf_xref WHERE product_id IN (SELECT product_id FROM #__{vm}_product WHERE vendor_id = ".$vendor_id.")";
        
        $db = new ps_DB;      
        $db->query( $q );
    }
    
    function deleteCategoryXref($vendor_id) {
        $q = "DELETE FROM #__{vm}_category_xref 
              WHERE category_child_id IN (SELECT category_id 
                                          FROM #__{vm}_product_category_xref 
                                          WHERE product_id IN (SELECT product_id FROM #__{vm}_product WHERE vendor_id = ".$vendor_id."))";
        
        $db = new ps_DB;      
        $db->query( $q );
    }
    
    function deleteCategory($vendor_id) {
        $q = "DELETE FROM #__{vm}_category 
              WHERE category_id IN (SELECT category_id 
                                    FROM #__{vm}_product_category_xref 
                                    WHERE product_id IN (SELECT product_id FROM #__{vm}_product WHERE vendor_id = ".$vendor_id."))";
        
        $db = new ps_DB;      
        $db->query( $q );
    }
    
    function deleteProductCategoryXref($vendor_id) {
        $q = "DELETE FROM #__{vm}_product_category_xref 
              WHERE product_id IN (SELECT product_id FROM #__{vm}_product WHERE vendor_id = ".$vendor_id.")";
        
        $db = new ps_DB;      
        $db->query( $q );
    }
    
    function deleteProduct($vendor_id) {
        $q = "DELETE FROM #__{vm}_product WHERE vendor_id = ".$vendor_id;
        
        $db = new ps_DB;      
        $db->query( $q );
    }
}

if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
    include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
    // Load the theme-user_class as extended
    include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
    // Otherwise we have to use the original classname to extend the core-class
    class ps_cleardata extends vm_ps_cleardata {}
}
?>
